package vip.xiaonuo.hontu.listener.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.hontu.listener.HongTuActivePushDataPublisher;
import vip.xiaonuo.modular.htpushdata.entity.HtPushData;
import vip.xiaonuo.modular.htpushdata.service.HtPushDataService;

import javax.annotation.Resource;


@RestController
@Slf4j
public class BizDataReceiveController {

    @Resource
    private HtPushDataService pushDataService;

    @Resource
    private HongTuActivePushDataPublisher publisher;

    @PostMapping("/bizDataReceive")
    private void receive(@RequestBody Object obj){
        // 先保存上报数据，后面再单独开一个线程去处理数据转换
        HtPushData data = new HtPushData();
        data.setStatus(0);
        data.setValue(JSON.toJSONString(obj));
        data.setPushFlag(Boolean.FALSE);
        pushDataService.save(data);
        log.debug("鸿图设备主动上报的数据 : {}", data.getId());

        publisher.pushEvent(data);
    }

}
